﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace register
{
    public partial class frmRegister : Form
    {
        public frmRegister()
        {
            InitializeComponent();
        }

        private void btnRegister_Click(object sender, EventArgs e)
        {
            bool res = false;
            string name = txtUesrNme.Text;
            string pwd = txtPwd.Text;
            string sql = string.Format("select * from user");
            SqlDataReader dbh = DBHelper.Select(sql);
            while (dbh.Read())
            {
                if (name != dbh["username"].ToString().Trim()&& pwd != dbh["userpwd"].ToString().Trim())
                {
                    string sql1 = "insert into Student1(username,userpwd) values('" + name + "','" + pwd + "')";
                    bool reader;
                    reader = DBHelper.GetNonQuery(sql1);
                    MessageBox.Show("注册成功");
                }
            }
            dbh.Close();


        }

        private void txtUesrNme_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void txtUesrNme_Leave(object sender, EventArgs e)
        {
            
            if (txtUesrNme.Text == "")
            {
                lblLeave.Text = "账号不能为空！";
            }
            else
            {
                
                string name = txtUesrNme.Text;
                
                string sql = string.Format("select * from user where username='{0}'",name);
                SqlDataReader dbh = DBHelper.Select(sql);
                while (dbh.Read())
                {
                        lblLeave.Text = "账号已被注册！";
                    break;
                }
                dbh.Close();
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void txtPwd_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtPwd_Leave(object sender, EventArgs e)
        {
            if (txtPwd.Text == "")
            {
                lblLeave1.Text = "密码不能为空！";
            }
            else
            {
                lblLeave1.Text = "";
            }
            
        }
    }
}
